#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <string.h>

int main() {
	    // 1. 创建共享内存
	         int shm_id = shmget(IPC_PRIVATE, 1024, 0666|IPC_CREAT);
	             
	                 // 2. 映射内存
	                     char *shm_ptr = (char*)shmat(shm_id, NULL, 0);
	                         
	                             // 3. 写入数据
	                                 strcpy(shm_ptr, "Shared Memory Data");
	                                     
	                                         // 4. 子进程读取
	                                             if (fork() == 0) {
	                                                     printf("Child read: %s\n", shm_ptr);
	                                                             shmdt(shm_ptr);
	                                                                     exit(0);
	                                                                         }
	                                                                             
	                                                                                 wait(NULL);
	                                                                                     shmdt(shm_ptr);
	                                                                                         shmctl(shm_id, IPC_RMID, NULL); // 删除共享内存
	                                                                                             return 0;
	                                                                                             }
